﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Xml;

namespace LegacyCommentMigration
{
    class Program
    {
        public static string connectionString = @"server=localhost;database=umbraco;user id=umbraco;password=umbraco";
        public static string umbracoConfig = "umbraco.config";
        public static SqlConnection c;

        static void Main(string[] args)
        {
            c = new SqlConnection(connectionString);
            c.Open();

            SqlCommand a = new SqlCommand("delete from Comment", c);
            a.ExecuteNonQuery();

            XmlDocument d = new XmlDocument();
            d.Load(umbracoConfig);
            parse(d.DocumentElement);
            // Thread.Sleep(5000);
        }

        public static void parse(XmlNode n)
        {
            foreach (XmlNode a in n.ChildNodes)
            {

                if (a.Name.Equals("node"))
                {
                    string type = a.Attributes["nodeTypeAlias"].Value;
                    if (type.Equals("umbracoBlogComment"))
                    {

                        Console.WriteLine(".");
                        int parentId = Convert.ToInt32(a.ParentNode.Attributes["id"].Value);
                        DateTime createDate = DateTime.Parse(a.Attributes["createDate"].Value);

                        string name = a.SelectSingleNode("./data[@alias='name']").FirstChild.Value;
                        string email = a.SelectSingleNode("./data[@alias='email']").FirstChild.Value;
                        string website = "";
                        try
                        {
                            website = a.SelectSingleNode("./data[@alias='website']").FirstChild.Value;
                        }
                        catch (Exception e) { }
                        string comment = a.SelectSingleNode("./data[@alias='comment']").FirstChild.Value;

                        SqlCommand cx = new SqlCommand(@"insert into Comment(mainid,nodeid,name,email,website,comment,spam,ham,created) 
                        values(@mainid,@nodeid,@name,@email,@website,@comment,@spam,@ham,@created)", c);

                        cx.Parameters.Add(new SqlParameter("@mainid", -1));
                        cx.Parameters.Add(new SqlParameter("@nodeid", parentId));
                        cx.Parameters.Add(new SqlParameter("@name", name));
                        cx.Parameters.Add(new SqlParameter("@email", email));
                        cx.Parameters.Add(new SqlParameter("@website", website));
                        cx.Parameters.Add(new SqlParameter("@comment", comment));
                        cx.Parameters.Add(new SqlParameter("@spam", false));
                        cx.Parameters.Add(new SqlParameter("@ham", true));
                        cx.Parameters.Add(new SqlParameter("@created", createDate));

                        cx.ExecuteNonQuery();
                    }
                }
                parse(a);
            }
        }
    }
}
